Skip to content

Make Scout models trait-only - #468

Merged
binaryfire merged 4 commits into
0.4from
scout/trait-only-searchable-models
Aug 3, 2026
Merged

Make Scout models trait-only#468
binaryfire merged 4 commits into
0.4from
scout/trait-only-searchable-models

Conversation

@binaryfire

@binaryfire binaryfire commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

This changes Scout so Eloquent models opt into search by using the Searchable trait alone. Applications no longer need to implement SearchableInterface.

This matches Laravel Scout model ergonomics while preserving Hypervel static analysis and coroutine safety.

Problem

Searchable already provides the full application-facing model behavior. Requiring models to also implement SearchableInterface exposes an internal typing detail and makes ordinary Scout setup more verbose than Laravel.

The interface was also used as a public generic bound across builders, engines, jobs, events, and collections. That made collection invariance difficult to express and pushed internal capability annotations into application boundaries.

Implementation

  • Keep SearchableInterface as an internal static-analysis shape.
  • Type public Scout boundaries on Eloquent Model, then narrow to the searchable capability only where a method is consumed.
  • Preserve the model collection type through makeSearchableUsing so applications can transform searchable collections without losing model information.
  • Update queued imports, removals, observers, scopes, events, commands, and all supported engines to use the same contract.
  • Make intentional loose Scout-key matching explicit for engines that return numeric keys as strings.
  • Convert framework fixtures to trait-only models and keep coverage for engine mapping, queued work, scope macros, and coroutine state.
  • Update the Scout documentation and search examples to show the trait-only setup.

Existing models that explicitly implement SearchableInterface continue to work. Search indexing and query behavior do not otherwise change.

Verification

The full formatting, static-analysis, parallel test, Testbench, and dogfood gates pass. Focused Scout engine, scope, and coroutine tests also pass.

Summary by CodeRabbit

  • New Features

    • Scout-searchable models can now use the Searchable trait without explicitly declaring an additional interface.
    • Broader model compatibility is supported across indexing, importing, syncing, and search engines.
  • Bug Fixes

    • Improved handling of numeric Scout keys, including keys represented as strings.
    • More reliable detection of soft-deleted models and search configuration values.
  • Documentation

    • Updated Scout examples and guidance to reflect the simpler model setup.

Treat the Searchable trait as the application-facing model contract while keeping SearchableInterface as an internal static-analysis shape.

Widen public engine, command, job, event, and collection boundaries to ordinary Eloquent models, then narrow only where Scout capabilities are consumed. This removes the need for application models to implement an extra interface and keeps the API aligned with Laravel Scout.

Preserve collection types through model hooks, remove redundant callback annotations, and make intentional loose Scout-key matching explicit where search services serialize numeric keys as strings.
Convert Scout's application-model fixtures to use the Searchable trait without implementing the internal interface.

Update engine mocks and scope/coroutine coverage around the widened model boundaries. Keep the Typesense removable-collection regression discriminating by mocking the stored key separately from the model's live Scout key.
Update the Scout and search documentation to show the Laravel-style model setup: extend Eloquent Model and use the Searchable trait.

Remove the internal interface from application examples and explain that the trait supplies the complete model capability used by Scout.
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@binaryfire, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 48 minutes

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 639e25f0-155c-40f6-9996-294a2ae8d0ac

📥 Commits

Reviewing files that changed from the base of the PR and between 833d7d0 and 4fba7a0.

📒 Files selected for processing (1)
  • src/scout/src/Contracts/SearchableInterface.php
📝 Walkthrough

Walkthrough

Scout now documents searchable models through the Searchable trait without explicit SearchableInterface declarations. Core PHPDoc and generic bounds use Model at public boundaries, with localized searchable assertions. Engine key and trait checks use updated comparison strictness.

Changes

Scout contract and typing updates

Layer / File(s) Summary
Model contracts and orchestration types
src/scout/src/Builder.php, src/scout/src/Contracts/SearchableInterface.php, src/scout/src/Engines/Engine.php, src/scout/src/Events/*, src/scout/src/Jobs/*, src/scout/src/Scout.php
Core annotations broaden model boundaries to Model and retain searchable intersections where needed.
Searchable runtime typing
src/scout/src/Searchable.php, src/scout/src/SearchableScope.php, src/scout/src/Traits/UniqueByScoutKeys.php
Searchable lookup, scope filtering, and key callbacks receive explicit model annotations and strict type checks.
Engine boundaries and checks
src/scout/src/Console/*, src/scout/src/Engines/*, src/scout/src/ModelObserver.php
Engine annotations use broader model collections. Trait and column checks use strict comparisons. Scout-key matching preserves the updated key-cast behavior.
Documentation and searchable fixtures
src/boost/docs/*.md, tests/Scout/Feature/*, tests/Scout/Models/*, tests/Scout/Unit/Engines/*
Examples and test models remove explicit SearchableInterface implementations. The Typesense deletion test uses a searchable model mock with configured Scout-key methods.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • hypervel/components#461: Both PRs modify Scout’s Searchable functionality, with different changes to typing and missing-key validation.
  • hypervel/components#464: Both PRs modify Scout builder, engines, queue/import lifecycles, and searchable model contracts.
  • hypervel/components#466: Both PRs modify Scout builder, engines, Searchable, and documentation.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely identifies the main change: Scout models now use the Searchable trait without explicitly implementing the interface.
Docstring Coverage ✅ Passed Docstring coverage is 96.43% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch scout/trait-only-searchable-models

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@greptile-apps

greptile-apps Bot commented Aug 3, 2026

Copy link
Copy Markdown

Greptile Summary

This PR makes Scout models searchable through the Searchable trait alone while retaining SearchableInterface as an internal static-analysis shape.

  • Widens builders, engines, jobs, events, commands, and collections to Eloquent model boundaries with local searchable-capability narrowing.
  • Updates engine key mapping and trait detection to account for serialized numeric keys and strict trait-name checks.
  • Converts Scout fixtures and documentation to the trait-only setup.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

Filename Overview
src/scout/src/Searchable.php Preserves the trait’s runtime behavior while refining collection/query annotations and strict membership checks.
src/scout/src/Contracts/SearchableInterface.php Recasts the interface as an internal searchable-model shape and widens engine-facing collection annotations.
src/scout/src/Builder.php Widens the public model generic while retaining a local intersection annotation where Scout methods are consumed.
src/scout/src/SearchableScope.php Narrows models locally before invoking searchable behavior and retains import event dispatch for complete chunks.
src/scout/src/Jobs/RemoveableScoutCollection.php Detects trait-only searchable models during queued-removal serialization and preserves their Scout keys.
src/scout/src/Jobs/RemoveFromSearch.php Continues restoring lightweight model instances keyed for deletion from external search engines.
src/scout/src/Engines/Engine.php Widens the abstract engine API to Eloquent models so trait-only models satisfy public boundaries.
src/scout/src/Engines/AlgoliaEngine.php Adapts engine annotations for trait-only models and explicitly supports numeric key representation differences.
src/scout/src/Engines/MeilisearchEngine.php Adapts update, deletion, mapping, and flush paths to widened model boundaries.
src/scout/src/Engines/TypesenseEngine.php Preserves searchable capability at consumption sites while accepting model-typed collections.
src/scout/src/Engines/CollectionEngine.php Preserves transformed searchable collections and handles key representation differences during mapping.
src/scout/src/Engines/DatabaseEngine.php Applies strict comparisons and widens no-op indexing boundaries without changing database search behavior.

Reviews (2): Last reviewed commit: "Use the imported model in Scout annotati..." | Re-trigger Greptile

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/scout/src/Contracts/SearchableInterface.php (1)

21-21: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Use the imported Model alias in the PHPDoc directive.

Model is already imported on Line 10. Replace the fully qualified class name on Line 21 with Model.

Proposed fix
- * `@phpstan-require-extends` \Hypervel\Database\Eloquent\Model
+ * `@phpstan-require-extends` Model

As per coding guidelines, import classes with use statements instead of referencing fully qualified class names.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/scout/src/Contracts/SearchableInterface.php` at line 21, Update the
`@phpstan-require-extends` directive in SearchableInterface to reference the
existing imported Model alias instead of the fully qualified
Hypervel\Database\Eloquent\Model name.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/scout/src/Contracts/SearchableInterface.php`:
- Line 21: Update the `@phpstan-require-extends` directive in SearchableInterface
to reference the existing imported Model alias instead of the fully qualified
Hypervel\Database\Eloquent\Model name.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 4666e7bb-6f12-4606-abdd-9807f43f415e

📥 Commits

Reviewing files that changed from the base of the PR and between 0a31249 and 833d7d0.

📒 Files selected for processing (39)
  • src/boost/docs/scout.md
  • src/boost/docs/search.md
  • src/scout/src/Builder.php
  • src/scout/src/Console/IndexCommand.php
  • src/scout/src/Console/QueueImportCommand.php
  • src/scout/src/Console/SyncIndexSettingsCommand.php
  • src/scout/src/Contracts/SearchableInterface.php
  • src/scout/src/Engines/AlgoliaEngine.php
  • src/scout/src/Engines/CollectionEngine.php
  • src/scout/src/Engines/DatabaseEngine.php
  • src/scout/src/Engines/Engine.php
  • src/scout/src/Engines/MeilisearchEngine.php
  • src/scout/src/Engines/TypesenseEngine.php
  • src/scout/src/Events/ModelsFlushed.php
  • src/scout/src/Events/ModelsImported.php
  • src/scout/src/Jobs/MakeSearchable.php
  • src/scout/src/Jobs/RemoveFromSearch.php
  • src/scout/src/Jobs/RemoveableScoutCollection.php
  • src/scout/src/ModelObserver.php
  • src/scout/src/Scout.php
  • src/scout/src/Searchable.php
  • src/scout/src/SearchableScope.php
  • src/scout/src/Traits/UniqueByScoutKeys.php
  • tests/Scout/Feature/CoroutineSafetyTest.php
  • tests/Scout/Feature/SearchableScopeTest.php
  • tests/Scout/Models/ConditionalSearchableModel.php
  • tests/Scout/Models/ConfigBasedTypesenseModel.php
  • tests/Scout/Models/CustomScoutKeyModel.php
  • tests/Scout/Models/FilteringSearchableModel.php
  • tests/Scout/Models/PrefixSearchableModel.php
  • tests/Scout/Models/SearchableModel.php
  • tests/Scout/Models/SoftDeletableSearchableModel.php
  • tests/Scout/Models/SoftDeleteSearchableModel.php
  • tests/Scout/Models/TypesenseSearchableModel.php
  • tests/Scout/Models/TypesenseSoftDeleteSearchableModel.php
  • tests/Scout/Models/UuidSearchableModel.php
  • tests/Scout/Unit/Engines/AlgoliaEngineTest.php
  • tests/Scout/Unit/Engines/MeilisearchEngineTest.php
  • tests/Scout/Unit/Engines/TypesenseEngineTest.php

Reference the existing Model import in the SearchableInterface PHPStan requirement. This keeps the internal contract consistent with repository import conventions without changing its behavior or type meaning.
@binaryfire
binaryfire merged commit b2c78c7 into 0.4 Aug 3, 2026
32 checks passed
@binaryfire
binaryfire deleted the scout/trait-only-searchable-models branch August 3, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant